package BookBorrow;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import BookDB.h2DAO;
import Loginout.UserBean;



@WebServlet("/Borrowbystaff")
public class Borrowbystaff extends HttpServlet {
	private static final long serialVersionUID = 2L;
       

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		resp.setContentType("text/html;charset=UTF-8");
		req.setCharacterEncoding("UTF-8");
	
	String bookid=req.getParameter("bookid");
	String userid=req.getParameter("userid");
	String isbn=req.getParameter("isbn");
	HttpSession session = req.getSession();
	UserBean UB = (UserBean) session.getAttribute("ub");
    String username=req.getParameter("username");
    UserBean UC = null;
	try {
		UC = h2DAO.SelectUser(username);
	} catch (SQLException e2) {
		e2.printStackTrace();
	}
	
	if (UB == null) {
		resp.sendRedirect("error.html");
	} else if (UB.vaild()) {
			try {
				PrintWriter writer=resp.getWriter();
					if (UC.getBorrowingbook()>=5)
					{
						writer.print("The user \""+username+ "\" have borrowed over the limited 5 books.Please remind him/her to return some books.");
					}
					else
					{
			int update=	h2DAO.updatebook4borrow(bookid,userid,isbn);
			if (update>0)
				resp.sendRedirect("BookMag.jsp");
			else
				writer.print("update borrow book record error!");
					}
		} catch (SQLException e) {
		
			e.printStackTrace();
		}
		
	}
	}

	
}
